import CommonUtils from "../btoolkit/CommonUtils";
import BButton from "../btoolkit/components/BButton";
import App from "../btoolkit/framework/App";

const { ccclass, property } = cc._decorator;

const AnswerConfig = [
    { option: [14, 10, 16, 12, 8], answer: 10 },
    { option: [32, 16, 22, 10, 15], answer: 16 },
    { option: [12, 16, 10, 18, 14], answer: 10 },
    { option: [11, 15, 14, 12, 13], answer: 14 },
    { option: [8, 13, 7, 9, 10], answer: 9 },
    { option: [6, 8, 4, 2, 3], answer: 2 },
    { option: [6, 5, 9, 3, 7], answer: 6 },
    { option: [9, 5, 6, 7, 8], answer: 9 },
    { option: [16, 13, 9, 11, 7], answer: 11 },
    { option: [14, 12, 8, 6, 11], answer: 11 },
    { option: [12, 10, 8, 6, 4], answer: 12 },
    { option: [12, 14, 16, 18, 20], answer: 14 },
    { option: [9, 5, 12, 14, 17], answer: 14 },
    { option: [6, 8, 9, 7, 5], answer: 9 },
    { option: [15, 17, 11, 14, 16], answer: 14 },
    { option: [15, 17, 9, 13, 12], answer: 12 },
    { option: [14, 13, 12, 11, 10], answer: 13 },
    { option: [2, 4, 6, 8, 10], answer: 8 },
    { option: [12, 14, 16, 20, 17], answer: 17 },
    { option: [14, 18, 16, 13, 17], answer: 17 }
];

@ccclass
export default class PopupQuestion extends cc.Component {

    @property(cc.Node)
    dialog: cc.Node = null;

    @property(cc.Node)
    btnClose: cc.Node = null;

    @property(cc.Sprite)
    question: cc.Sprite = null;

    static instance: PopupQuestion = null;

    private btnOptionLabel: cc.Label[] = [];
    private questionIndex: number;
    private onClose: Function;
    private thisObj: any;
    private isCorrectAnswer: boolean

    static show(onClose: Function, thisObj?: any) {
        if (PopupQuestion.instance) {
            console.error('不能重复创建PopupQuestion');
            return;
        }
        PopupQuestion.instance = cc.instantiate(App.instance.resPreloader.getRes('prefabs/common/PopupQuestion')).getComponent(PopupQuestion);
        App.instance.node.addChild(PopupQuestion.instance.node);
        PopupQuestion.instance.onClose = onClose;
        PopupQuestion.instance.thisObj = thisObj;
    }

    onDestroy() {
        PopupQuestion.instance = null;
        console.log('PopupQuestion被删除')
        if (this.onClose) {
            console.log('PopupQuestion被删除，触发onClose()')
            this.onClose.call(this.thisObj, this.isCorrectAnswer);
        }
    }

    onLoad() {
        CommonUtils.setTouchMask(this.node);
        this.dialog.active = false;

        for (let i = 0; i < 5; i++) {
            let btnOption = this.dialog.getChildByName('Answer_' + i);
            this.btnOptionLabel.push(btnOption.getChildByName('Label').getComponent(cc.Label));
            BButton.addButton(btnOption, i).setTouchUpEvent(this.onAnswerCli, this);
        }

        //关闭按钮
        BButton.addButton(this.btnClose).setTouchUpEvent(() => {
            this.isCorrectAnswer = false;
            this.node.destroy();
        });

        //问题和答案
        this.refreshQuestion();
    }

    private refreshQuestion() {
        this.questionIndex = Math.floor(Math.random() * 20);
        this.question.spriteFrame = App.instance.resPreloader.getRes('textures/common/children_limit').getSpriteFrame('tm' + (this.questionIndex + 1));
        for (let i = 0; i < 5; i++) {
            this.btnOptionLabel[i].string = AnswerConfig[this.questionIndex].option[i] + '';
        }

        this.dialog.scale = 0.5;
        this.dialog.active = true;
        cc.tween(this.dialog).to(0.3, { scale: 1 }, { easing: 'backOut' }).start();
    }

    private onAnswerCli(e: cc.Event.EventTouch, i: number) {
        let selectValue = AnswerConfig[this.questionIndex].option[i];
        if (selectValue == AnswerConfig[this.questionIndex].answer) {
            this.isCorrectAnswer = true;
            this.node.destroy();
        } else {
            this.refreshQuestion();
        }
    }

}
